package com.teradyne.merlin2.ui.server.service.helper;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

import com.teradyne.merlin2.business.service.webservice.PartRequestDetail;
import com.teradyne.merlin2.ui.shared.models.OrderHeader;
import com.teradyne.merlin2.ui.shared.models.OrderLine;
import com.teradyne.merlin2.ui.shared.models.OrderRequest;

public class OrderRequestHelper {
	
	private static SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");

	
	public OrderRequestHelper() {
		// TODO Auto-generated constructor stub
	}
	
	public static ArrayList<OrderRequest> morph(com.teradyne.merlin2.business.service.webservice.OrderRequest[] businessListRequest) {
		ArrayList<OrderRequest> listOrderRequest = new ArrayList<OrderRequest>();
		for(com.teradyne.merlin2.business.service.webservice.OrderRequest businessOrderRequest : businessListRequest) {
			OrderRequest orderRequest = new OrderRequest();
			
			orderRequest.setBpelInstanceNo(businessOrderRequest.getBpelInstanceNumber() != null ? businessOrderRequest.getBpelInstanceNumber() : "");
			orderRequest.setCreatedBy(businessOrderRequest.getCreatedBy() != null ? businessOrderRequest.getCreatedBy() : "");
			orderRequest.setDateCreated(businessOrderRequest.getCreatedDate() != null ? dateFormat.format(businessOrderRequest.getCreatedDate().getTime()) : "");
			orderRequest.setOrderRequestStatus(businessOrderRequest.getStatus() != null ? businessOrderRequest.getStatus().getStatus() : "");
			orderRequest.setOrderRequestNo((int) businessOrderRequest.getRequestId());
			orderRequest.setOrderRequestId((int) businessOrderRequest.getRequestId());
			orderRequest.setUserId(businessOrderRequest.getCreatedBy() != null ? businessOrderRequest.getCreatedBy() : "");
			
			ArrayList<OrderHeader> listOrderHeader = new ArrayList<OrderHeader>();
			for(com.teradyne.merlin2.business.service.webservice.PartRequestHeader businessHeader : businessOrderRequest.getPartRequestHeaders()) {
				OrderHeader orderHeader = new OrderHeader();
				
				orderHeader.setOrderHeaderId((int) businessHeader.getPartRequestHeaderId());
				orderHeader.setOrderHeaderNo(String.valueOf(businessOrderRequest.getCustomerNumber()));
				orderHeader.setCustomerNo((int) businessOrderRequest.getCustomerNumber());
				orderHeader.setTesterDown(businessHeader.getTesterDownFlag().equals("T") ? true : false);
				orderHeader.setAttentionFirstName(businessHeader.getAttentionFirstName() != null ? businessHeader.getAttentionFirstName() : "");
				orderHeader.setAttentionLastName(businessHeader.getAttentionLastName() != null ? businessHeader.getAttentionLastName() : "");
				orderHeader.setAttentionPhoneNo(businessHeader.getAttentionPhone() != null ? businessHeader.getAttentionPhone() : "");
				orderHeader.setCostCenter(businessHeader.getTerCostCenter() != null ? businessHeader.getTerCostCenter() : "");
				orderHeader.setPaymentMethod(businessHeader.getPaymentMethod() != null ? businessHeader.getPaymentMethod() : "");
				orderHeader.setOrderRequestNo(businessHeader.getPartRequestHeaderNumber() != null ? businessHeader.getPartRequestHeaderNumber() : "");
				orderHeader.setOrderHeaderId((int) businessHeader.getPartRequestHeaderId());
				orderHeader.setSerialNo(businessHeader.getSystemSerialNumber() != null ? businessHeader.getSystemSerialNumber() : "");
				orderHeader.setOrderHeaderStatus(businessHeader.getStatus() != null ? businessHeader.getStatus().getStatus() : "");
				orderHeader.setSpecialInstruction(businessHeader.getSpecialInstructions() != null ? businessHeader.getSpecialInstructions() : "");
		
				final ArrayList<OrderLine> listOrderLine = new ArrayList<OrderLine>();
				for (PartRequestDetail partRequestDetail : businessHeader.getPartRequestDetailList()) {
					OrderLine orderLine = new OrderLine();
		
					orderLine.setPartNo(partRequestDetail.getPartNumber() != null ? partRequestDetail.getPartNumber() : "");
					orderLine.setServiceType(partRequestDetail.getServiceType() != null ? partRequestDetail.getServiceType() : "");
					orderLine.setOrderLineNo(partRequestDetail.getRequestDetailLineNo().intValue());
					orderLine.setPartDesc(partRequestDetail.getRequestedTerPartDescn() != null ? partRequestDetail.getRequestedTerPartDescn() : "");
					orderLine.setQuantity(partRequestDetail.getRequestQuantity().intValue());
					orderLine.setOrderLineId((int) partRequestDetail.getPartRequestDetailId());
					orderLine.setSystemSerialNumber(partRequestDetail.getSystemSerialNumber());
					orderLine.setPartSerialNumber(partRequestDetail.getAvailableTerPartSerialNumb());
					orderLine.setStatusOrderLine(partRequestDetail.getStatus() != null ? partRequestDetail.getStatus().getStatusInternal() : "");
					orderLine.setExternalStatusOrderLine(partRequestDetail.getStatus() != null ? partRequestDetail.getStatus().getStatusExternal() : "");
					orderLine.setStatusIdOrderLine(partRequestDetail.getStatus() != null ? partRequestDetail.getStatus().getStatus() : "");
					orderLine.setAgreementNo(partRequestDetail.getAgreementNumber() != null ? partRequestDetail.getAgreementNumber() : "");
					orderLine.setAgreementType(partRequestDetail.getAgreementTypeCode() != null ? partRequestDetail.getAgreementTypeCode() : "");
					orderLine.setControlNo(partRequestDetail.getControlNumber() != null ? partRequestDetail.getControlNumber() : "");
					orderLine.setLineNo(partRequestDetail.getLineNumber() != null ? partRequestDetail.getLineNumber() : "");
					orderLine.setServiceDescription(partRequestDetail.getServiceTypeDescn() != null ? partRequestDetail.getServiceTypeDescn() : "");
					orderLine.setRepairCode(partRequestDetail.getTerPartRepairType() != null ? partRequestDetail.getTerPartRepairType() : "");
					orderLine.setSystemType(partRequestDetail.getRequestDetailSystemType() != null ? partRequestDetail.getRequestDetailSystemType() : "");
					orderLine.setNotes(partRequestDetail.getRequestDetailComments() != null ? partRequestDetail.getRequestDetailComments() : "");
					orderLine.setInvoiceComment(partRequestDetail.getSpecialInvoiceComment() != null ? partRequestDetail.getSpecialInvoiceComment() : "");
					orderLine.setCoordinatorComment(partRequestDetail.getSpecialInvoiceComment() != null ? partRequestDetail.getSpecialInvoiceComment() : "");
					orderLine.setOemPartSerialNumber(partRequestDetail.getRequestedOemPartSerialNo() != null ? partRequestDetail.getRequestedOemPartSerialNo() : "");
					
					orderLine.setTeradynePlatform(businessHeader.getTerBusinessGroup() != null ? businessHeader.getTerBusinessGroup() : "");
		
					listOrderLine.add(orderLine);
				}
				orderHeader.setOrderLines(listOrderLine);
				listOrderHeader.add(orderHeader);
			}
			orderRequest.setOrderHeaders(listOrderHeader);
			listOrderRequest.add(orderRequest);
		}
		
		return listOrderRequest;
	}
	
}
